package com.umeox.watch.moto.launcher.adapter;


import static com.umeox.watch.moto.launcher.bean.NotificationBean.TYPE_CHAT;
import static com.umeox.watch.moto.launcher.bean.NotificationBean.TYPE_PTT_CHAT;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.NotificationBean;
import com.umeox.watch.moto.launcher.swipedismiss.ItemTouchHelperAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rambo 2019-07-05
 * Describe:
 */
public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter {

    private List<NotificationBean> mDatas = new ArrayList<>();
    private Context mContext;
    private final LayoutInflater mLayoutInflater;

    private CancelSwipeEvent cancelSwipeEvent;

    private OnItemClickListener mOnItemClickListener;

    public void setCancelSwipeEventListener(CancelSwipeEvent listener) {
        this.cancelSwipeEvent = listener;
    }


    public NotificationAdapter(Context context, OnItemClickListener listener) {
        this.mContext = context;
        mOnItemClickListener = listener;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        return new NotificationViewHolder(mLayoutInflater.inflate(R.layout.layout_notification_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        NotificationBean notificationBean = mDatas.get(position);
        int notifyType = notificationBean.getType();
        final NotificationViewHolder holder = (NotificationViewHolder) viewHolder;
        final TextView itemCountTv = holder.itemCountTv;
        final TextView itemTypeTv = holder.itemTypeTv;
        final TextView itemNameTv = holder.itemNameTv;
        String intervalTime = DateUtils.getIntervalTimeFormat(mContext, notificationBean.getTime());
        itemTypeTv.setText(intervalTime);
        itemNameTv.setText(notificationBean.getTitle());
        final int count = notificationBean.getTimes().size();
        switch (notifyType) {
            case TYPE_CHAT:
            case TYPE_PTT_CHAT:
                itemCountTv.setText(mContext.getString(R.string.nft_messages));
                final Drawable drawable = mContext.getResources().getDrawable(R.drawable.chat_unread_flag);
                itemCountTv.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
                break;
            case NotificationBean.TYPE_CALL:
            case NotificationBean.TYPE_CALL_UNKNOWN:
                itemCountTv.setText(mContext.getString(R.string.nft_missed_call));
                final Drawable callMissed = mContext.getResources().getDrawable(R.drawable.contact_call_missed);
                itemCountTv.setCompoundDrawablesRelativeWithIntrinsicBounds(callMissed, null, null, null);
                break;
        }

        holder.itemLl.setOnClickListener(v -> mOnItemClickListener.onItemClick(notificationBean));

    }

    @Override
    public int getItemCount() {
        final int size = mDatas.size();
        mOnItemClickListener.onRefreshEmptyView(size == 0);
        return size;
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {

    }

    @Override
    public void onItemDismiss(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
        if(mOnItemClickListener != null){
            mOnItemClickListener.notifyItemRemoved(position);
        }
    }

    class NotificationViewHolder extends RecyclerView.ViewHolder {

        TextView itemTypeTv;
        TextView itemNameTv;
        TextView itemCountTv;
        LinearLayout itemLl;

        public NotificationViewHolder(@NonNull View itemView) {
            super(itemView);
            itemTypeTv = itemView.findViewById(R.id.notification_type_tv);
            itemNameTv = itemView.findViewById(R.id.name_tv);
            itemCountTv = itemView.findViewById(R.id.count_tv);
            itemLl = itemView.findViewById(R.id.item_ll);
        }
    }


    public List<NotificationBean> getDatas() {
        return mDatas;
    }

    public interface CancelSwipeEvent {

        void onSwipeEventCanceled();
    }

    public interface OnItemClickListener {
        void onItemClick(NotificationBean notificationBean);

        void onRefreshEmptyView(boolean empty);

        void notifyItemRemoved(int position);
    }
}
